goog.provide('GLL.events.Rotate');

goog.require('GLL');
goog.require('goog.events.EventTarget');
goog.require('lime.animation.MoveTo');


/**
 * Object representing Rotate interaction.
 * @constructor
 * @param {lime.events.Event} event Event that started Rotating.
 * @extends goog.events.EventTarget
 */
GLL.events.Rotate = function(event) {

    goog.base(this);

    this.target = event.targetObject;

	//old mouse position
	this.last_mouse_x = event.screenPosition.x;
	this.last_mouse_y = event.screenPosition.y;
	this._rotation = this.target.getRotation();

    event.swallow(GLL.EVENT_MM, goog.bind(this.RotateHandler_, this));
    event.swallow(GLL.EVENT_MU, goog.bind(this.releaseHandler_, this));

    this.dispatchEvent(new goog.events.Event(GLL.events.Rotate.Event.START));
};

goog.inherits(GLL.events.Rotate, goog.events.EventTarget);

/**
 * Enum for Rotating related events
 * @enum {string}
 */
GLL.events.Rotate.Event = {
    START: 'start',
    ROTATE: 'rotate',
    END: 'end',
};

/**
 * @inheritDoc
 */
//GLL.events.Rotate.prototype.disposeInternal = function() {
//    goog.base(this, 'disposeInternal');
//};

/**
 * Handle move events.
 * @private
 * @param {lime.events.Event} e Event.
 */
GLL.events.Rotate.prototype.RotateHandler_ = function(e) {
    var pos = e.screenPosition;
	var y_chng = this.last_mouse_y - pos.y;
	var rot = this._rotation + y_chng;
	//console.log('chng:' + y_chng + ' rot:'+rot);
    this.target.setRotation(rot);
	var event = new goog.events.Event(GLL.events.Rotate.Event.ROTATE);
	event.rotation = rot;
    this.dispatchEvent(event); 
};

/**
 * Handle release events.
 * @private
 * @param {lime.events.Event} e Event.
 */
GLL.events.Rotate.prototype.releaseHandler_ = function(e) {
    this.dispatchEvent(new goog.events.Event(GLL.events.Rotate.Event.END));
    lime.scheduleManager.callAfter(this.dispose, this, 100);
};